const apiStatus = require('../../apiStatus');
const validHelper = require('../../helpers/validHelper');
const cartService = require('./cartService');

function setup(app, appRouter){
    appRouter.post('/api/cart/list', async(ctx,next) => {
        let appId = 2;//ctx.appInfo.id;
        let userId = 1;//ctx.user.id;
        let pageIndex = ctx.request.body.pageIndex || 0;
	      var pageSize = ctx.request.body.pageSize || 10;

        pageSize = Math.min(100,Math.max(1,pageSize));
	      let offset = pageIndex * pageSize;
	      let limit = pageSize;

        const rows = await cartService.list(appId,userId,offset,limit);
        ctx.apiBody = rows;
        next();
    });

    appRouter.post('/api/cart/add', async(ctx,next) => {
        let appId = 2;//ctx.appInfo.id;
        let userId = 1;//ctx.user.id;
        let productId = ctx.request.body.productId;
        let skuId = ctx.request.body.skuId;
      	let count = ctx.request.body.count;
      	let amount = ctx.request.body.amount;

      	let miss = validHelper.check([
      	               productId,"productId",
                       skuId,"skuId",
      	               count,"count",
      	               amount,"amount"
      	           ]);
      	if(miss.length > 0) {
          ctx.apiBody = {apiStatus: apiState.missArguments(miss)};
      		return;
      	}

        let blag = await cartService.add(appId,userId,productId,skuId,count,amount);
        ctx.apiBody = {apiStatus: blag ? apiStatus.success : apiStatus.serverError};
        //ctx.apiStatus = blag ? apiStatus.success : apiStatus.serverError;
        next();
    });

    appRouter.post('/api/cart/edit', async(ctx,next) => {
        let appId = 2;//ctx.appInfo.id;
        let userId = 1;//ctx.user.id;
        let productId = ctx.request.body.productId;
        let skuId = ctx.request.body.skuId;
      	let type = ctx.request.body.type;

      	let miss = validHelper.check([
      	               productId,"productId",
                       skuId,"skuId",
      	               type,"type"
      	           ]);
      	if(miss.length > 0) {
          ctx.apiBody = {apiStatus: apiState.missArguments(miss)};
      		return;
      	}
        let blag = await cartService.update(userId,productId,skuId,type);
        console.log(blag);
        ctx.apiBody = {apiStatus: blag ? apiStatus.success : apiStatus.serverError};
        next();
    });

    appRouter.post('/api/cart/del', async(ctx,next) => {
        let appId = 2;//ctx.appInfo.id;
        let userId = 1;//ctx.user.id;
        let productId = ctx.request.body.productId;
        let skuId = ctx.request.body.skuId;
      	let miss = validHelper.check([
      	               productId,"productId",
                       skuId,"skuId"
      	]);
      	if(miss.length > 0) {
          ctx.apiBody = {apiStatus: apiState.missArguments(miss)};
      		return;
      	}
        let blag = await cartService.del(userId,productId,skuId);
        console.log(blag);
        ctx.apiBody = {apiStatus: blag ? apiStatus.success : apiStatus.serverError};
        next();
    });

    appRouter.post('/api/cart/selectList', async(ctx,next) => {
        let appId = 2;//ctx.appInfo.id;
        let userId = 1;//ctx.user.id;
        let skuIds = ctx.request.body.skuIds;
        const rows = await cartService.getSelectCart(appId,userId,skuIds);
        ctx.apiBody = rows;
        next();
    });

}

module.exports = setup;
